home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 742 / rkrm_lib2 / rkrm_lib2.lha / Exec_Library / Memory / allocate.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  3KB  |  80 lines

  1. ;/* allocate.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 allocate.c
  3. Blink FROM LIB:c.o,allocate.o TO allocate LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit ;
  5.  
  6. allocate.c - example of allocating and using a private memory pool.
  7.  
  8.  
  9. Copyright (c) 1992 Commodore-Amiga, Inc.
  10.  
  11. This example is provided in electronic form by Commodore-Amiga, Inc. for
  12. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  13. published by Addison-Wesley (ISBN 0-201-56774-1).
  14.  
  15. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  16. information on the correct usage of the techniques and operating system
  17. functions presented in these examples.  The source and executable code
  18. of these examples may only be distributed in free electronic form, via
  19. bulletin board or as part of a fully non-commercial and freely
  20. redistributable diskette.  Both the source and executable code (including
  21. comments) must be included, without modification, in any copy.  This
  22. example may not be published in printed form or distributed with any
  23. commercial product.  However, the programming techniques and support
  24. routines set forth in these examples may be used in the development
  25. of original executable software products for Commodore Amiga computers.
  26.  
  27. All other rights reserved.
  28.  
  29. This example is provided "as-is" and is subject to change; no
  30. warranties are made.  All use is at your own risk. No liability or
  31. responsibility is assumed.
  32. */
  33.  
  34. #include <exec/types.h>
  35. #include <exec/memory.h>
  36. #include <clib/exec_protos.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39.  
  40. #ifdef LATTICE
  41. int CXBRK(void)  { return(0); }  /* Disable Lattice CTRL/C handling */
  42. void chkabort(void) { return; }  /* really */
  43. #endif
  44.  
  45. #define BLOCKSIZE 4000     /* or whatever you need */
  46.  
  47. VOID main(VOID)
  48. {
  49.     struct MemHeader *mh;
  50.     struct MemChunk  *mc;
  51.     APTR   block1, block2;
  52.  
  53.     /* Get the MemHeader needed to keep track of our new block. */
  54.     mh = (struct MemHeader *)AllocMem((LONG)sizeof(struct MemHeader), MEMF_CLEAR);
  55.     if (!mh) exit(10);
  56.  
  57.     /* Get the actual block the above MemHeader will manage. */
  58.     if ( !(mc = (struct MemChunk *)AllocMem(BLOCKSIZE, 0)) )
  59.     {
  60.         FreeMem(mh, (LONG)sizeof(struct MemHeader));
  61.         exit(10);
  62.     }
  63.     mh->mh_Node.ln_Type = NT_MEMORY;
  64.     mh->mh_First        = mc;
  65.     mh->mh_Lower        = (APTR)mc;
  66.     mh->mh_Upper        = (APTR)(BLOCKSIZE + (ULONG)mc);
  67.     mh->mh_Free         = BLOCKSIZE;
  68.  
  69.     mc->mc_Next  = NULL;                     /* Set up first chunk in the freelist */
  70.     mc->mc_Bytes = BLOCKSIZE;
  71.  
  72.     block1 = (APTR)Allocate(mh,20);
  73.     block2 = (APTR)Allocate(mh, 314);
  74.  
  75.     printf("Our MemHeader struct at $%lx. Our block of memory at $%lx\n", mh, mc);
  76.     printf("Allocated from our pool: block1 at $%lx, block2 at $%lx\n", block1, block2);
  77.  
  78.     FreeMem(mh, (LONG)sizeof(struct MemHeader));
  79.     FreeMem(mc, (LONG)BLOCKSIZE);
  80. }